package com.ruoyi.develop.factory.factoryMaterialAssist.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.factory.factoryMaterialMain.domain.FactoryMaterialMain;
import com.ruoyi.develop.factory.factoryProduceWork.domain.FactoryProduceWork;
import com.ruoyi.develop.factory.factoryProduceWork.mapper.FactoryProduceWorkMapper;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import com.ruoyi.develop.project.domain.Project;
import com.ruoyi.develop.project.mapper.ProjectMapper;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.mapper.ProjectBaseInfoMapper;
import com.ruoyi.export.service.IExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.factory.factoryMaterialAssist.mapper.FactoryMaterialAssistMapper;
import com.ruoyi.develop.factory.factoryMaterialAssist.domain.FactoryMaterialAssist;
import com.ruoyi.develop.factory.factoryMaterialAssist.service.IFactoryMaterialAssistService;

/**
 * 原材料（辅材）Service业务层处理
 *
 * @author lmm
 * @date 2024-11-06
 */
@Service
public class FactoryMaterialAssistServiceImpl implements IFactoryMaterialAssistService {
    @Resource
    private FactoryMaterialAssistMapper factoryMaterialAssistMapper;

    @Resource
    private FactoryProduceWorkMapper factoryProduceWorkMapper;

    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private ProjectBaseInfoMapper projectBaseInfoMapper;
    @Resource
    private LvDaohuoDetailMapper lvDaohuoDetailMapper;

    @Autowired
    private IExportService exportService;

    /**
     * 查询原材料（辅材）
     *
     * @param id 原材料（辅材）主键
     * @return 原材料（辅材）
     */
    @Override
    public FactoryMaterialAssist selectById(String id) {
        return factoryMaterialAssistMapper.selectById(id);
    }

    /**
     * 查询原材料（辅材）列表
     *
     * @param entity 原材料（辅材）
     * @return 原材料（辅材）
     */
    @Override
    public List<FactoryMaterialAssist> selectList(FactoryMaterialAssist entity) {
        return factoryMaterialAssistMapper.selectList(entity);
    }

    /**
     * 新增原材料（辅材）
     *
     * @param entity 原材料（辅材）
     * @return 结果
     */
    @Override
    public int insert(FactoryMaterialAssist entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }

        if(StringUtils.isNotBlank(entity.getWorkOrderId())) {
            FactoryProduceWork workOrder =  factoryProduceWorkMapper.selectById(entity.getWorkOrderId());
            if (StringUtils.isNotBlank(workOrder.getProNo())) {
                Project project = projectMapper.selectProjectByCode(workOrder.getProNo());
                entity.setProjectId(project.getId());
                entity.setProjectCode(project.getCode());
            }
            if (StringUtils.isNotBlank(workOrder.getMaterialsCode())) {
                entity.setGoodsId(workOrder.getMaterialsCode());
                GoodsOrderIn goods = goodsOrderInMapper.selectByCode(workOrder.getMaterialsCode());
                entity.setGoodsId(goods.getId());
                entity.setGoodsName(goods.getName());
                entity.setGoodsModel(goods.getModel());
            }
        }


        if(StringUtils.isNotBlank(entity.getDaohuoDetailId())) {
            LvDaohuoDetail lvDaohuoDetail = lvDaohuoDetailMapper.selectById(entity.getDaohuoDetailId());
            if(lvDaohuoDetail!=null && StringUtils.isNotBlank(lvDaohuoDetail.getProjectId())){
                ProjectBaseInfo order = projectBaseInfoMapper.selectById(lvDaohuoDetail.getProjectId());
                entity.setProjectId(order.getProject().getId());
                entity.setProjectCode(order.getProject().getCode());
            }
            GoodsOrderIn goods = goodsOrderInMapper.selectById(lvDaohuoDetail.getGoodId());
            entity.setGoodsId(goods.getId());
            entity.setGoodsName(goods.getName());
            entity.setGoodsModel(goods.getModel());
            entity.setProducer(lvDaohuoDetail.getSupplierName());
            entity.setGoodsSaleId(lvDaohuoDetail.getGoodsSaleId());
        }


        if(StringUtils.isBlank(entity.getBatchNumber())){
            String todayDate = DateUtils.dateTime();
            String maxBatchNumber = factoryMaterialAssistMapper.getMaxBatchNumberByDate(todayDate);
            // 如果存在 则加1 否则返回0001
            if (maxBatchNumber != null) {
                maxBatchNumber =  Long.parseLong(maxBatchNumber) + 1+"";
            }
            else {
                maxBatchNumber = todayDate+"0001";
            }
            entity.setBatchNumber(maxBatchNumber);
        }

        if(StringUtils.isBlank(entity.getSampleNumber())){
            String todayDate = DateUtils.dateTime();
            String maxSampleNumber = factoryMaterialAssistMapper.getMaxSampleNumberByDate(todayDate);
            // 如果存在 则加1 否则返回0001
            if (maxSampleNumber != null) {
                maxSampleNumber =  Long.parseLong(maxSampleNumber) + 1+"";
            }
            else {
                maxSampleNumber = todayDate+"0001";
            }
            entity.setSampleNumber(maxSampleNumber);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        generateInspectReport(entity);
        return factoryMaterialAssistMapper.insert(entity);
    }

    /**
     * 修改原材料（辅材）
     *
     * @param entity 原材料（辅材）
     * @return 结果
     */
    @Override
    public int update(FactoryMaterialAssist entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        generateInspectReport(entity);
        return factoryMaterialAssistMapper.update(entity);
    }

    /**
     * 批量删除原材料（辅材）
     *
     * @param ids 需要删除的原材料（辅材）主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return factoryMaterialAssistMapper.deleteByIds(ids);
    }

    /**
     * 删除原材料（辅材）信息
     *
     * @param id 原材料（辅材）主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return factoryMaterialAssistMapper.deleteById(id);
    }

    public void updateRukumingxiByDaohuo(String daohuoDetailId, String rukuMingxiId) {
        factoryMaterialAssistMapper.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
    }


    /** 根据质检数据生成质检报告 */
    public void generateInspectReport(FactoryMaterialAssist entity){
        Map<String, Object> params = new HashMap<>();
        params.put("entity", entity);
        params.put("inspectDate", DateUtils.formatDate(entity.getArriveInspectDate(),"yyyy年MM月dd日"));
        String fileName = "辅材检验报告.docx";
        String templateName = "辅材检验报告.docx";
        String url = exportService.convertDocxAndUpload(fileName, params, templateName);
        entity.setMaterialReport(url);
    }






}
